/**
 * modified by 张周海 in 2012-01-17
 * 
 * @class DicCombo
 * @extends Ext.form.ComboBox
 */
DicCombo = Ext.extend(Ext.form.ComboBox, {
	useForSelect : false,
	constructor : function(_config) {
		if (_config) {
			if (_config == null) {
				_config = {};
			}
		}
		Ext.apply(this, _config);

		this.store = new Ext.data.JsonStore( {
			url : 'node/getNodeAllByTypeId',
			root : 'data',
			totalProperty : 'totalProperty',
			fields : [ 'nodeName', 'nodeId' ],
			baseParams : {
				typeId : this.typeId
			},
			listeners : {
				load : function() {
					if (this.useForSelect) {
						var rec = new Ext.data.Record( {
							valueContent : '',
							valueName : '全部'
						});
						this.store.add(rec);
					}
				},
				scope : this
			}
		});
		this.resultTpl = new Ext.XTemplate('<tpl for=".">' + '<div class="x-combo-list-item">' + '<div style="float:left">' + '{nodeId}' + '</div>' + '<div style="float:right">' + '{nodeName}' + '</div>'
				+ '<div style="clear:both"></div>' + ' </div></tpl>');
		DicCombo.superclass.constructor.call(this, {
			minChars : 0,
			store : this.store,
			displayField : 'nodeId',
			tpl : this.resultTpl,
			valueField : 'nodeId',
			triggerAction : 'all',
			editable : true,
			emptyText : '请选择',
			forceSelection : true
		});
	},
	onSelect : function(record, index) {
		if (this.fireEvent('beforeselect', this, record, index) !== false) {
			this.setValueFromCache(record.data[this.valueField || this.displayField]);
			this.collapse();
			this.fireEvent('select', this, record, index);
		}
	},
	setValueFromCache : function(v) {
		DicCombo.superclass.setValue.call(this, v);
	},
	setValue : function(v) {
		var text = v;
		if (this.mode == 'local') {
			if (this.valueField) {
				var r = this.findRecord(this.valueField, v);
				if (r) {
					text = r.data[this.displayField];
				} else if (Ext.isDefined(this.valueNotFoundText)) {
					text = this.valueNotFoundText;
				}
			}
			this.lastSelectionText = text;
			if (this.hiddenField) {
				this.hiddenField.value = Ext.value(v, '');
			}
			Ext.form.ComboBox.superclass.setValue.call(this, text);
			this.value = v;
			return this;
		} else {
			var r = this.findRecord(this.valueField, v);
			if (r) {
				text = r.data[this.displayField];
			} else if (Ext.isDefined(this.valueNotFoundText)) {
				text = this.valueNotFoundText;
			}
			this.lastSelectionText = text;
			if (this.hiddenField) {
				this.hiddenField.value = Ext.value(v, '');
			}
			Ext.form.ComboBox.superclass.setValue.call(this, text);
			this.value = v;
			return this;
		}
	},
	doQuery : function(q, forceAll, noSelect) {
		q = Ext.isEmpty(q) ? '' : q;
		var qe = {
			query : q,
			forceAll : forceAll,
			combo : this,
			cancel : false
		};
		if (this.fireEvent('beforequery', qe) === false || qe.cancel) {
			return false;
		}
		q = qe.query;
		forceAll = qe.forceAll;
		if (forceAll === true || (q.length >= this.minChars)) {
			if (this.lastQuery !== q) {
				this.lastQuery = q;
				if (this.mode == 'local') {
					this.selectedIndex = -1;
					if (forceAll) {
						this.store.clearFilter();
					} else {
						this.store.filter( [ {
							fn : function(record) {
								var value = this.store.data.createValueMatcher(q, true, true, false);
								return value.test(record.get(this.displayField)) || value.test(record.get(this.valueField));
							},
							scope : this
						} ]);
					}
					this.onLoad();
				} else {
					this.store.baseParams['inputValue'] = q;
					this.store.noSelect = noSelect;
					this.store.load( {
						params : this.getParams(q)
					});
					if (!noSelect) {
						this.expand();
					}
				}
			} else {
				this.selectedIndex = -1;
				this.onLoad();
			}
		}
	}
});
Ext.reg('dicCombo', DicCombo);
